package com.lucence.a_helloword;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

/**
 * ctrl+T
 * ctrl+shift+g
 * 
 * @author Administrator
 *
 */
public class HelloWorld {
	@Test
	public void index () throws Exception{
		FSDirectory directory = FSDirectory.open(new File("./indexDir/"));
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
		IndexWriter indexWriter = new IndexWriter(directory, config);
		Document doc = new Document();
		doc.add(new Field("id", "i3 1am au strude", Store.YES, Index.ANALYZED_NO_NORMS));
		doc.add(new Field("id", "i4 2am at teaqche", Store.YES, Index.ANALYZED));
		doc.add(new Field("id", "i5 3am a5 7sde", Store.YES, Index.ANALYZED));
		doc.add(new Field("id", "i6 4am ag str", Store.YES, Index.ANALYZED));
		indexWriter.addDocument(doc);
		indexWriter.close();
	}
	
	/**搜索索引库
	 * @throws Exception */
	@Test
	public void searchIndex() throws Exception{
		List<Article> list = new ArrayList<Article>();
	
		Directory directory = FSDirectory.open(new File("./indexDir/"));
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
		IndexSearcher indexSearcher = new IndexSearcher(IndexReader.open(directory));
		
		String queryString = "lucene 是 全文 检索 框架";
//		QueryParser queryParser = new QueryParser(Version.LUCENE_36,"title",analyzer);
//		QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_36,new String[]{"title","content"},analyzer);
//		Query query = queryParser.parse(queryString);
		Query query = new TermQuery(new Term("id", "i4"));
		TopDocs topDocs = indexSearcher.search(query, 100);
		System.out.println("总记录数："+topDocs.totalHits);
		//返回结果集
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		//遍历
		if(scoreDocs!=null && scoreDocs.length>0){
			for(int i=0;i<scoreDocs.length;i++){
				ScoreDoc scoreDoc = scoreDocs[i];
				System.out.println("得分:"+scoreDoc.score);
				// 获取惟一编号
				int doc = scoreDoc.doc;
				//使用内部惟一的编号，获取对应的Document数据对象
				Document document = indexSearcher.doc(doc);
				//将Document转换成Article
				Article article = new Article();
				article.setId(document.get("id"));
				article.setTitle(document.get("title"));
				article.setContent(document.get("content"));
				list.add(article);
			}
		}
		indexSearcher.close();
		//模拟页面操作
		if(list!=null && list.size()>0){
			for(Article article:list){
				System.out.println("文章编号:"+article.getId());
				System.out.println("文章标题:"+article.getTitle());
				System.out.println("文章内容:"+article.getContent());
				System.out.println("--------------------------------------------");
			}
		}
	}
	
}
